package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfOrderDAO;
import com.ljkj.common.db.dao.ZjfOrderItemDAO;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.domain.ZjfOrder;
import com.ljkj.common.db.domain.ZjfOrdersItem;
import com.ljkj.common.db.domain.ZjfUser;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfOrderItemService {
	
	private ZjfOrderItemDAO zjfOrderItemDAO;
	private TLogService tLogService;
	private ZjfGoodsTypeService zjfGoodsTypeService;


	public String getOrderItemsByProperties(String params,String orderId,int currentPage,int pageSize,String sortField,String sortOrder){
		 
		Class[] pojos = {ZjfOrdersItem.class};
		String[] propertyNames= {"orderId"};
		Object[] values= {orderId};
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfOrderItemDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfOrderItemDAO.setPageMaxCount(pageSize);
		List<ZjfOrdersItem> zjfOrdersItems = this.zjfOrderItemDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfOrdersItem zjfOrdersItem : zjfOrdersItems){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("goodsName",zjfOrdersItem.getGoodsName());
			String goodTypeId = zjfOrdersItem.getGoodsTypeId();
			if(goodTypeId != null && !"".equals(goodTypeId)){
				ZjfGoodsType zjfGoodsType = this.zjfGoodsTypeService.geZjfGoodsTypeById(goodTypeId);
				if(zjfGoodsType != null){
					entry_map.put("goodsTypeName",zjfGoodsType.getTypeName());
				}
			}
			entry_map.put("goodsBn",zjfOrdersItem.getGoodsBn());
			entry_map.put("orderCostItem",zjfOrdersItem.getOrderCostItem());
			entry_map.put("goodsPrice",zjfOrdersItem.getGoodsPrice());
			entry_map.put("goodsAmount",zjfOrdersItem.getGoodsAmount());
			entry_map.put("orderItmeDlyStatus",zjfOrdersItem.getOrderItmeDlyStatus());
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

	public void setZjfGoodsTypeService(ZjfGoodsTypeService zjfGoodsTypeService) {
		this.zjfGoodsTypeService = zjfGoodsTypeService;
	}

	public void setZjfOrderItemDAO(ZjfOrderItemDAO zjfOrderItemDAO) {
		this.zjfOrderItemDAO = zjfOrderItemDAO;
	}

}
